﻿package com.dbm.model;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.dbm.util.DbUtil;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class ConnPoolFactory {
	private static ComboPooledDataSource ds = null;
	private static ComboPooledDataSource dsdbm = null;
	private static ComboPooledDataSource dsctl = null;
	private static DbUtil dbUtil = null;
	
	static {
		Logger log = Logger.getLogger(ConnPoolFactory.class);
		log.setLevel(Level.WARN);
		ds = new ComboPooledDataSource();
		dsdbm = new ComboPooledDataSource();
		dsctl = new ComboPooledDataSource();
		try {
			dbUtil = new ModelProp().getJdbcUrlByProp();
			// 设置JDBC的Driver类
			ds.setDriverClass(dbUtil.getWmsDriverClass());
			dsdbm.setDriverClass(dbUtil.getDriverClass());
			dsctl.setDriverClass(dbUtil.getCtlDriverClass());
			// 参数由 Config
			// 设置JDBC的URL
			ds.setJdbcUrl(dbUtil.getWmsUrl());
			dsdbm.setJdbcUrl(dbUtil.getUrl());
			dsctl.setJdbcUrl(dbUtil.getCtlUrl());
			// 设置数据库的登录用户名
			ds.setUser(dbUtil.getWmsUsername());
			dsdbm.setUser(dbUtil.getUsername());
			dsctl.setUser(dbUtil.getCtlUsername());
			// 设置数据库的登录用户密码
			ds.setPassword(dbUtil.getWmsPassword());
			dsdbm.setPassword(dbUtil.getPassword());
			dsctl.setPassword(dbUtil.getCtlPassword());
			// 设置连接池的最大连接数
			ds.setMaxPoolSize(200);
			dsdbm.setMaxPoolSize(200);
			dsctl.setMaxPoolSize(200);
			// 设置连接池的最小连接数
			ds.setMinPoolSize(15);
			dsdbm.setMinPoolSize(15);
			dsctl.setMinPoolSize(15);

			ds.setInitialPoolSize(10);
			dsdbm.setInitialPoolSize(10);
			dsctl.setInitialPoolSize(10);
			
			ds.setCheckoutTimeout(3000);//最长等待时间  
			dsdbm.setCheckoutTimeout(3000);//最长等待时间  
			dsctl.setCheckoutTimeout(3000);//最长等待时间  
			
//			ds.setMaxStatements(30000);
//			dsdbm.setMaxStatements(30000);
//			dsctl.setMaxStatements(30000);  
			ds.setIdleConnectionTestPeriod(60);
			dsdbm.setIdleConnectionTestPeriod(60);
			dsctl.setIdleConnectionTestPeriod(60);
//			//每次get connection 的时候，验证连接的有效性
//			ds.setTestConnectionOnCheckout(true);
//			dsdbm.setTestConnectionOnCheckout(true);
//			dsctl.setTestConnectionOnCheckout(true);
			
			ds.setPreferredTestQuery("select sysdate from dual");
			dsdbm.setPreferredTestQuery("select sysdate from dual");
			dsctl.setPreferredTestQuery("select sysdate from dual");
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("加载数据库连接池信息失败！！");
		}
	}
	/**
	 * 获取wms数据库连接
	 * @return
	 */
	public static synchronized Connection getWMSConnection() {
		Connection conn = null;
		try {
			conn = ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
//		if(conn==null){
//			return getWMSConnection();
//		}
		return conn;
	}
	/**
	 * 获取dbm数据库连接
	 * @return
	 */
	public static synchronized Connection getDBMConnection() {
		Connection conn = null;
		try {
			conn = dsdbm.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
//		if(conn==null){
//			return getDBMConnection();
//		}
		return conn;
	}
	/**
	 * 获取CTL 数据库连接
	 * @return
	 */
	public static synchronized Connection getCtlConnection(){
		Connection conn = null;
		try {
			System.out.println("开始获取数据库连接。");
			conn = dsctl.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
//		if(conn==null){
//			return getCtlConnection();
//		}
		return conn;
	}
}
